package com.kotlin.mvpdemo.ui.main;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.kotlin.mvpdemo.R;
import com.kotlin.mvpdemo.base.BaseActivity;
import com.kotlin.mvpdemo.databinding.ActivityMainBinding;
import com.kotlin.mvpdemo.entity.ApiResponse;
import com.kotlin.mvpdemo.entity.ChaptersEntity;
import com.kotlin.mvpdemo.ui.message.MessageFragment;
import com.kotlin.mvpdemo.ui.mine.MineFragment;
import com.kotlin.mvpdemo.ui.users.UsersFragment;

import java.util.ArrayList;

/**
 * author : fang.zhang
 * e-mail : initzf@126.com
 * date   : 2020/6/6
 * desc   : 主Activity
 */
public class MainActivity extends BaseActivity<MainView, MainPresenter, ActivityMainBinding> implements MainView {
    private static final String TAG = "MainActivity";
    private int selectItem = 0;
    private Fragment mContentFragment;

    private UsersFragment mUsersFragment;
    private MessageFragment mMessageFragment;
    private MineFragment mMineFragment;

    @Override
    protected MainPresenter getPresenter() {
        return new MainPresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected ActivityMainBinding getViewBind() {
        return ActivityMainBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        /*final NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupWithNavController(mViewBinding.mBottomNavigation, navController);
        mViewBinding.mBottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if (getSupportFragmentManager().isStateSaved()) {
                    return false;
                }
                if (selectItem == item.getItemId()) {
                    return false;
                }
                selectItem = item.getItemId();
                NavigationUI.onNavDestinationSelected(item, navController);
                return true;
            }
        });*/

        BottomNavigationView mBottomNavigation = mViewBinding.mBottomNavigation;
        mBottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if (getSupportFragmentManager().isStateSaved()) {
                    return false;
                }
                if (selectItem == item.getItemId()) {
                    return false;
                }
                switch (item.getItemId()) {
                    case R.id.users_fragment:
                        if (mUsersFragment == null) {
                            mUsersFragment = (UsersFragment) getSupportFragmentManager().findFragmentByTag(UsersFragment.class.getSimpleName());
                        }
                        if (mUsersFragment == null) {
                            mUsersFragment = UsersFragment.newInstance();
                        }
                        changeFragment(mUsersFragment);
                        break;
                    case R.id.message_fragment:
                        if (mMessageFragment == null) {
                            mMessageFragment = (MessageFragment) getSupportFragmentManager().findFragmentByTag(MessageFragment.class.getSimpleName());
                        }
                        if (mMessageFragment == null) {
                            mMessageFragment = MessageFragment.newInstance();
                        }
                        changeFragment(mMessageFragment);
                        break;
                    case R.id.mine_fragment:
                        if (mMineFragment == null) {
                            mMineFragment = (MineFragment) getSupportFragmentManager().findFragmentByTag(MineFragment.class.getSimpleName());
                        }
                        if (mMineFragment == null) {
                            mMineFragment = MineFragment.newInstance();
                        }
                        changeFragment(mMineFragment);
                        break;
                    default:
                }
                selectItem = item.getItemId();
                return true;
            }
        });
        mBottomNavigation.setSelectedItemId(R.id.users_fragment);
        mBottomNavigation.setSelected(true);
    }


    private void changeFragment(Fragment mFragment) {
        if (mFragment == mContentFragment) {
            return;
        }
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (!mFragment.isAdded()) {
            if (mContentFragment == null) {
                fragmentTransaction.add(R.id.nav_host_fragment_container, mFragment, mFragment.getClass().getSimpleName()).commit();
            } else {
                fragmentTransaction.hide(mContentFragment).add(R.id.nav_host_fragment_container, mFragment, mFragment.getClass().getSimpleName()).commit();
            }
        } else {
            fragmentTransaction.hide(mContentFragment).show(mFragment).commit();
        }
        mContentFragment = mFragment;
    }


    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume: ");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG, "onPause: ");
    }

    @Override
    public void changeText(String str) {
        Log.i(TAG, "changeText: " + str);
    }

    @Override
    public void getListData(ArrayList<ChaptersEntity> list) {
        for (ChaptersEntity chaptersEntity : list) {
            Log.i(TAG, "getListData: " + chaptersEntity.getName());
        }
        mPresenter.test();
    }

    @Override
    public void showError(ApiResponse response) {
        Toast.makeText(this, "" + response.msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void dismiss() {
        Log.i(TAG, "dismiss: 停止加载");
    }
}
